package com.dongdongshop.service.impl;

import com.dongdongshop.mapper.TbSpecificationMapper;
import com.dongdongshop.mapper.TbSpecificationOptionMapper;
import com.dongdongshop.pojo.*;
import com.dongdongshop.service.SpecificationService;
import com.dongdongshop.vo.SpecVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

@Service
@DubboService
public class SpecificationServiceImpl implements SpecificationService {

    @Resource
    private TbSpecificationMapper specificationMapper;

    @Resource
    private TbSpecificationOptionMapper optionMapper;


    //查询全部规格
    @Override
    public List<TbSpecification> findAll(String specName) {
        TbSpecificationExample specificationExample = new TbSpecificationExample();
        if (specName != null && !"".equals(specName)) {
            TbSpecificationExample.Criteria criteria = specificationExample.createCriteria();
            criteria.andSpecNameLike("%"+specName+"%");
        }
        return specificationMapper.selectByExample(specificationExample);
    }

    //增加方法   1:先增加TbSpecification 并返回主键 2:在根据返回的主键去增加TbSpecificationOption
    @Override
    @Transactional
    public void insertSpecAndOption(TbSpecification specification, List<TbSpecificationOption> optionList) {


        specificationMapper.insertResultId(specification);

        optionList.stream().forEach(option -> option.setSpecId(specification.getId()));


        optionList.stream().forEach(option -> optionMapper.insert(option));
    }

    //回显操作
    @Override
    public SpecVo specificationService(Long id) {
        SpecVo vo = new SpecVo();
        vo.setSpecification(specificationMapper.selectByPrimaryKey(id));

        TbSpecificationOptionExample tbSpecificationOptionExample = new TbSpecificationOptionExample();
        TbSpecificationOptionExample.Criteria criteria = tbSpecificationOptionExample.createCriteria();
        criteria.andSpecIdEqualTo(id);
        vo.setOptionList(optionMapper.selectByExample(tbSpecificationOptionExample));
        return vo;
    }

    //修改操作
    @Override
    @Transactional
    public void updateSpecAndOption(TbSpecification specification, List<TbSpecificationOption> optionList) {
        specificationMapper.updateByPrimaryKey(specification);

        //先删除
        TbSpecificationOptionExample optionExample = new TbSpecificationOptionExample();
        TbSpecificationOptionExample.Criteria criteria = optionExample.createCriteria();
        criteria.andSpecIdEqualTo(specification.getId());
        optionMapper.deleteByExample(optionExample);

        //在增加
        optionList.stream().forEach(option -> option.setSpecId(specification.getId()));
        optionList.stream().forEach(option -> optionMapper.insert(option));
    }

    //删除操作
    @Override
    @Transactional
    public void deleteBrand(Long[] ids) {

        List<Long> longs = Arrays.asList(ids);

        //删除规格表
        TbSpecificationExample tbSpecificationExample = new TbSpecificationExample();
        TbSpecificationExample.Criteria criteria = tbSpecificationExample.createCriteria();
        criteria.andIdIn(longs);
        specificationMapper.deleteByExample(tbSpecificationExample);

        //删除规格详细表
        TbSpecificationOptionExample optionExample = new TbSpecificationOptionExample();
        TbSpecificationOptionExample.Criteria criteria1 = optionExample.createCriteria();
        criteria1.andSpecIdIn(longs);
        optionMapper.deleteByExample(optionExample);

    }
}
